/*
 * Copyright 2008-2010 GP Software
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * MockSchedulerClient.java | Created on Sep 1, 2010
 */
package org.gp.scheduler.mock;

import org.gp.scheduler.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The mock scheduler client
 *
 * @author Paul Augustine C. Mendoza
 */
public final class MockSchedulerClient {

    private static final Logger LOGGER = LoggerFactory
            .getLogger(MockSchedulerClient.class);
    private static final int WAIT_TIME = 5000;  // 5 seconds

    private MockSchedulerClient() {
        // Do nothing...
    }

    public static void main(String[] args) {
        MockSchedulerClient client = new MockSchedulerClient();
        try {
            LOGGER.info("===== Starting bootstrap scheduler =====");
            client.runBootstrapScheduler();
            // Pause
            LOGGER.info("==============================");
            LOGGER.info("Wait for the next scheduler...");
            LOGGER.info("==============================");
            Thread.sleep(WAIT_TIME);

            LOGGER.info("===== Starting adhoc scheduler =====");
            client.runAdhocScheduler();
        } catch (ScheduledJobException e) {
            LOGGER.error(e.getMessage());
        } catch (InterruptedException e) {
            LOGGER.error(e.getMessage());
        }
    }

    private void runBootstrapScheduler() throws ScheduledJobException,
            InterruptedException {
        JobScheduler scheduler = new BootstrapJobScheduler();
        runScheduler(scheduler);
    }

    private void runAdhocScheduler() throws ScheduledJobException,
            InterruptedException {
        SchedulerParameter schedulerParameter = MockSchedulerParameterUtil
                .createSchedulerParameter();
        JobScheduler scheduler = new AdhocJobScheduler(schedulerParameter);
        runScheduler(scheduler);

    }

    private void runScheduler(JobScheduler scheduler) throws
            ScheduledJobException, InterruptedException {
        scheduler.start();
        Thread.sleep(WAIT_TIME);
        scheduler.stop();
    }
}
